package com.example.appkcsj.slice;

import com.example.appkcsj.DAO.FamilyDao;
import com.example.appkcsj.DAO.FamilyDaoImpl;
import com.example.appkcsj.ResourceTable;
import com.example.appkcsj.dbutils.DBUtils;
import com.example.appkcsj.entity.Family;
import com.example.appkcsj.entity.Student;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class fourthAbilitySlice extends AbilitySlice implements DatePicker.ValueChangedListener,Component.ClickedListener {
    private final static HiLogLabel HI_LOG_LABEL =
            new HiLogLabel(HiLog.LOG_APP, 0x00201, "fourthAbilitySlice");
    private TextField name1;
    private TextField name;
    private TextField phone;
    private TextField age;
    private TextField address;
    private Text birth;
    private Button b, b1;
 //   private DBUtils1 dbUtils1;
    private DatePicker d;

    private FamilyDao familyDao;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_fourth);

        familyDao = new FamilyDaoImpl(this);

        //dbUtils1 = new DBUtils1(fourthAbilitySlice.this);
        name1 = (TextField) findComponentById(ResourceTable.Id_field6);
        name = (TextField) findComponentById(ResourceTable.Id_field7);
        phone = (TextField) findComponentById(ResourceTable.Id_field8);
        age = (TextField) findComponentById(ResourceTable.Id_field9);
        address = (TextField) findComponentById(ResourceTable.Id_field10);
        d = (DatePicker) findComponentById(ResourceTable.Id_data);
        b = (Button) findComponentById(ResourceTable.Id_but4);
        birth = (TextField) findComponentById(ResourceTable.Id_text6);
        d.setValueChangedListener(this);
        b.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (component == b) {
                    if ("".equals(name1.getText()) || "".equals(name.getText()) || "".equals(phone.getText())
                            || "".equals(age.getText()) || "".equals(address.getText())) {
                        ToastDialog td = new ToastDialog(fourthAbilitySlice.this);
                        td.setText("请输入相应信息！");
                        td.show();
                    } else {
                        Family family = new Family(name1.getText()
                                , name.getText()
                                , phone.getText()
                                , age.getText()
                                , address.getText()
                                , birth.getText());
//                        boolean flag = dbUtils1.insertFamily(family);

                        boolean flag = familyDao.insert(family);


                        if (flag) {
                            HiLog.info(HI_LOG_LABEL, "--插入数据->>" + flag);
                            ToastDialog td = new ToastDialog(fourthAbilitySlice.this);
                            td.setText("保存成功！");
                            td.show();
                            Intent m = new Intent();
                            m.setParam("name1", name1.getText());
                            m.setParam("name", name.getText());
                            m.setParam("phone", phone.getText());
                            m.setParam("age", age.getText());
                            m.setParam("address", address.getText());
                            m.setParam("birth", birth.getText());
                            present(new fourthAbilitySlice(), m);
                            Operation operation = new Intent.OperationBuilder()
                                    .withDeviceId("")
                                    .withBundleName("com.example.appkcsj")//跳转到哪个应用
                                    .withAbilityName("com.example.appkcsj.thirdAbility")//要跳转的包名
                                    .build();//将上面的信息打包
                            m.setOperation(operation);
                            startAbility(m);
                        } else {
                            HiLog.info(HI_LOG_LABEL, "--插入数据->>" + flag);
                            ToastDialog td = new ToastDialog(fourthAbilitySlice.this);
                            td.setText("保存失败！");
                            td.show();
                        }


                    }
                }
            }
        });


    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onValueChanged(DatePicker d, int year, int month, int day) {
      birth.setText(year+"年"+month+"月"+day+"日");
    }

    @Override
    public void onClick(Component component) {

    }
}

